package org.skyeye.modeling.figures;

import org.eclipse.draw2d.EllipseAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;


public class BorderAnchor extends EllipseAnchor {
	IFigure figure = null;
	public BorderAnchor(IFigure figure) {
		super(figure);
		this.figure = figure;
	}
	
	public Point getLocation(Point reference) {
		
		Point p;
		p = getOwner().getBounds().getCenter();
		getOwner().translateToAbsolute(p);
		
		Rectangle rect = figure.getBounds();
		
		boolean isSelected = false;
		
		
		if(reference.x> rect.x && reference.x < (rect.x+rect.width) ){
			
			if(reference.y <= rect.y){
				p = ((DeviceFigure)getOwner()).getTopAnchorLor();
				isSelected = true;
			}else if(reference.y >= (rect.y+ rect.height)){
				p = ((DeviceFigure)getOwner()).getBottomAnchorLor();
				isSelected = true;
			}
		}else{
			if(reference.x <= rect.x){
				p = ((DeviceFigure)getOwner()).getLeftAnchorLoc();
				isSelected = true;
			}else if(reference.x>= (rect.x+rect.width)){
				p = ((DeviceFigure)getOwner()).getRightAnchorLoc();
				isSelected = true;
			}
		}
		
		if(!isSelected){
			p = ((DeviceFigure)getOwner()).getLeftAnchorLoc();
		}
		
		getOwner().translateToAbsolute(p);
		return p;

	}
}
